package cn.mothin.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import cn.mothin.entity.Organization;
import cn.mothin.frame.core.base.BaseDao;
import cn.mothin.frame.core.interfaces.IEntityDisplay;
import cn.mothin.frame.util.StringUtil;

@Repository("0b8cf761-5ec9-4788-ba3f-66c4ce2b9a08")
public class OrganizationDao extends BaseDao<Organization> implements
        IEntityDisplay {

    @Override
    public String queryDisplay(String id) {
        return prepareForQueryDisplay(id, "organizationName");
    }

    public List<Organization> queryOrganizations(String parentId) {
        if (StringUtil.isNullOrEmpty(parentId)) {
            String sql = "SELECT * FROM organization WHERE deleted<>TRUE AND parentId is null ";
            return this.query(sql);
        } else {
            String sql = "SELECT * FROM organization WHERE deleted<>TRUE AND parentId = ?";
            return this.query(sql, parentId);
        }

    }

    /**
     * 查询该部门的子部门
     * 
     * @param id
     * @return
     */
    public List<String> queryChild(String id) {
        String sql = "SELECT id FROM organization WHERE deleted <> TRUE AND parentId = ? ";
        return sqlHelper.queryForList(String.class, sql, id);
    }

    /**
     * 查询该组织下的人员
     * 
     * @param organizationId
     * @return
     */
    public List<String> querySystemUser(String organizationId) {
        String sql = "SELECT id FROM system_user WHERE deleted <> TRUE AND organizationId = ? ";
        return sqlHelper.queryForList(String.class, sql, organizationId);
    }

}
